import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
    public boolean containsDuplicate(int[] nums) {

        /*for (int i = 0; i < nums.length-1; i++) {
            for (int j = i+1; j < nums.length; j++) {
                if(nums[i] == nums[j]){
                    return true;
                }
            }
        }
        return false;*/
        Arrays.sort(nums);
        for (int i = 0; i < nums.length-1; i++) {
            if(nums[i] == nums[i+1]){
                return true;
            }
        }
        return false;
    }

    /*public int maxI(int[] nums){
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] >= 0){
                return i;
            }
        }
        return -1;
    }*/
    public int maxSubArray(int[] nums) {
        //1
        /*int max = nums[0];

        for (int i = 0; i < nums.length; i++) {
            int ret = nums[i];
            if(ret > max){
                max = ret;
            }
            for (int j = i+1; j < nums.length; j++) {
                ret += nums[j];
                if(ret > max){
                    max = ret;
                }
            }
        }
        return max;*/

        //2
        /*int len = nums.length;
        int[] dp = new int[len];
        dp[0] = nums[0];

        for (int i =1; i < len; i++) {
            dp[i] = Math.max(dp[i-1]+nums[i], nums[i]);
        }

        int res = dp[0];
        for (int d:
             dp) {
            if(d > res){
                res = d;
            }

        }
        return res;*/

        //3
        int pre = 0;
        int res = nums[0];

        for (int num:
             nums) {
            pre = Math.max(num, pre+num);
            res = Math.max(pre, res);
        }
        return res;
    }

    public int removeDuplicates(int[] nums) {
        /*for (int i = 0; i < nums.length-1; i++) {
            if(nums[i] == nums[i+1]){
                for (int j = i; j < nums.length; j++) {
                    nums[j] = nums[j+1];
                }

            }
        }*/

        if(nums == null || nums.length == 0){
            return 0;
        }

        int left=0;
        for (int right = 1; right < nums.length; right++) {
            if(nums[left] != nums[right]){
                nums[++left] = nums[right];
            }
        }
        System.out.println(++left);
        return 1;

    }
    public int maxProfit(int[] prices) {

        if(prices == null || prices.length <2){
            return 0;
        }
        /*int len = prices.length;
        int[][] dp = new int[len][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0];

        for (int i = 1; i < len; i++) {
            dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1]+prices[i]);
            dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0]-prices[i]);
        }

        return dp[len-1][0];*/

        int len = prices.length;
        int hold = -prices[0];
        int noHold = 0;

        for (int i = 1; i < len; i++) {
            hold = Math.max(hold, noHold-prices[i]);
            noHold = Math.max(noHold, hold+prices[i]);
        }
        return noHold;

    }
    public int[] intersect(int[] nums1, int[] nums2) {
        int pos1=0,pos2=0;
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        List<Integer> list = new ArrayList<>();

        while (pos1 < nums1.length && pos2 < nums2.length){
            if(nums1[pos1] < nums2[pos2]){
                pos1++;
            } else if (nums1[pos1] > nums2[pos2]) {
                pos2++;
            }else {
                list.add(nums1[pos1]);
                pos1++;
                pos2++;
            }
        }

        int[] res = new int[list.size()];
        int index = 0;
        for (int i = 0; i < list.size(); i++) {
            res[index++] = list.get(i);
        }
        return res;
    }
}
public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums1 = {7,6,4,3,1};
        int[] nums2 = {1, 2, 3};
        System.out.println(solution.intersect(nums1,nums2));
    }
}
